package logic

import (
	"2210a/commod/commodrpc/commodrpc"
	"2210a/commod/commodrpc/internal/svc"
	"2210a/commod/models/mysql"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type ShopDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopDelLogic {
	return &ShopDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopDelLogic) ShopDel(in *commodrpc.ShopDelRequest) (*commodrpc.ShopDelResponse, error) {
	// todo: add your logic here and delete this line
	var products mysql.Products
	mysql.Db.Where("id = ?", in.Id).Find(&products)
	if products.ID == 0 {
		return nil, fmt.Errorf("商品不存在，删除失败")
	}
	if products.UserId != in.UserId {
		return nil, fmt.Errorf("用户不存在，删除失败")
	}
	mysql.Db.Debug().Model(&products).Delete(&products)
	return &commodrpc.ShopDelResponse{}, nil
}
